package com.itheima.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.behaviour.dto.FollowBehaviorDto;
import com.itheima.common.util.RequestContext;
import com.itheima.user.dto.UserRelationDto;
import com.itheima.user.mapper.ApUserFanMapper;
import com.itheima.user.mapper.ApUserFollowMapper;
import com.itheima.user.pojo.ApUserFan;
import com.itheima.user.pojo.ApUserFollow;
import com.itheima.user.service.ApUserFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * APP用户关注信息表 服务实现类
 * </p>
 *
 * @author 作者
 * @since 2021-08-20
 */
@Service
public class ApUserFollowServiceImpl extends ServiceImpl<ApUserFollowMapper, ApUserFollow> implements ApUserFollowService {

    @Autowired
    private ApUserFollowMapper apUserFollowMapper;

    @Autowired
    private ApUserFanMapper apUserFanMapper;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    /***
     * 关注、取关操作
     * @param dto
     */
    @Override
    public void follow(UserRelationDto dto) {
        //游客无法执行关注或者取关->判断用户是否已登录
        //1:关注
        Integer userId = RequestContext.getNodeValue("id"); //用户ID（登录系统的用户|点击关注某作者的用户|小红的Id）
        String userName = RequestContext.getNodeValue("name"); //用户的name（登录系统的用户|点击关注某作者的用户|小红的name）
        //1.1:判断operation==1
        if(dto.getOperation()==1){
            //1.2:添加关注列表信息
            ApUserFollow follow = new ApUserFollow();
            follow.setUserId(userId);
            follow.setFollowId(dto.getAuthorId());  //文章作者的ID|班长的ID
            follow.setFollowName(dto.getAuthorName()); //文章作者的name|班长的name
            follow.setLevel(0);
            follow.setIsNotice(0);
            follow.setCreatedTime(LocalDateTime.now());
            apUserFollowMapper.insert(follow);

            //1.3:粉丝添加（给文章的作者添加粉丝|班长）
            ApUserFan fan = new ApUserFan();
            fan.setUserId(dto.getAuthorId());
            fan.setFansId(userId);  //当前执行关注作者的ID|小红的ID
            fan.setFansName(userName); //当前执行关注作者的name|小红的name
            fan.setLevel(0);
            fan.setCreatedTime(LocalDateTime.now());
            fan.setIsDisplay(0);
            fan.setIsShieldLetter(0);
            fan.setIsShieldComment(0);
            apUserFanMapper.insert(fan);

            //关注或者取关发消息，记录行为[userId(equipmentId：游客不能使用该功能)、authorId、articleId]
            FollowBehaviorDto followBehaviorDto = new FollowBehaviorDto();
            followBehaviorDto.setUserId(userId); //用户ID
            followBehaviorDto.setFollowId(dto.getAuthorId()); //作者ID
            followBehaviorDto.setArticleId(dto.getArticleId()); //文章ID
            //执行消息发送
            kafkaTemplate.send("followQueue", JSON.toJSONString(followBehaviorDto));
        }else{
            //2:取关
            //2.1:当前用户（小红）的关注列表移除作者（班长）
            QueryWrapper<ApUserFollow> followQueryWrapper = new QueryWrapper<ApUserFollow>();
            followQueryWrapper.eq("user_id",userId); //当前用户的ID
            followQueryWrapper.eq("follow_id",dto.getAuthorId());
            apUserFollowMapper.delete(followQueryWrapper);

            //2.2:将当前用户（小红）从作者（班长）的粉丝列表中移除
            QueryWrapper<ApUserFan> fanQueryWrapper = new QueryWrapper<ApUserFan>();
            fanQueryWrapper.eq("user_id",dto.getAuthorId()); //作者ID|班长ID
            fanQueryWrapper.eq("fans_id",userId); //要移除的粉丝ID|小红的ID
            apUserFanMapper.delete(fanQueryWrapper);
        }
    }
}
